package logic

import (
	"context"
	"fmt"
	"strconv"
	"taobao/goods_srv/user"

	"taobao/api/internal/svc"
	"taobao/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateAvatarInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateAvatarInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateAvatarInfoLogic {
	return &UpdateAvatarInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateAvatarInfoLogic) UpdateAvatarInfo(req *types.UserImageRequest) (resp *types.RegMsgResponse, err error) {
	// todo: 修改用户头像

	userID := l.ctx.Value("ID").(string)
	unin, _ := strconv.Atoi(userID)
	fmt.Print("id为..............", unin)
	_, err = l.svcCtx.UserSrv.UpdateAvatarInfo(l.ctx, &user.UserAvatarRequest{
		UserID: int64(unin),
		Avatar: req.Avatar,
	})
	if err != nil {
		return &types.RegMsgResponse{
			Cre:  -1,
			Mag:  "修改失败",
			Data: nil,
		}, err
	}

	return &types.RegMsgResponse{
		Cre:  0,
		Mag:  "修改成功",
		Data: nil,
	}, err
}
